#include <Debug/log.hpp>
#include <ShareMemory/allocator.hpp>

#define MYFS_DEBUG
#include <Debug/assert.h>
#define P(x)                                            \
  {                                                     \
    std::cout << #x << ":" << x << " func:" << __func__ \
              << " line:" << __LINE__ << "\n";          \
    system("pause");                                    \
  }

using namespace demo::debug::log;
using namespace multiprocess::shm_allocator;
struct person {
  int age = 10;
  int name = 10;
};  // 8bytes
int main() {
  info("shm allocator test");
  ShareMemoryPool shm_pool(123, 20, 4);  // 40 - 4 - 1 bytes
  shm_pool.debug_str();

  info("shm_pool alloc {} bytes", sizeof(person));
  char* p = (char*)shm_pool.alloc(sizeof(person));

  info("shm_pool data read");
  ((person*)p)->age = 10;
  ((person*)p)->name = 10;
  shm_pool.debug_str();
  auto per = (person*)shm_pool.data();
  std::cout << per->age << " " << per->name << "\n";

  info("allocator test");
  ShareMemoryAllocator<int> int_allocator(&shm_pool);
  shm_pool.debug_str();

  info("allocator alloc 3 int");
  int* int_ptr = int_allocator.allocate(3);
  shm_pool.debug_str();

  ShareMemoryAllocator<char> char_alloc(&shm_pool);
  info("allocator alloc 2 char");
  char_alloc.allocate(2);
  shm_pool.debug_str();

  info("allocator dealloc 3 int");
  int_allocator.deallocate(int_ptr, 3);
  shm_pool.debug_str();

  info("log test");

  println("println test, no args");
  println("hi I'm {00}, living in {}. I'm {} years old.", "name", "address",
          1000);
  trace("hi I'm { d}, living in {}. I'm {} years old.", "name", "address",
        1000);
  info("hi I'm { d}, living in {}. I'm {} years old.", "name", "address", 1000);
  debug("hi I'm {ab}, living in {}. I'm {} years old.", "name", "address",
        1000);
  warn("hi I'm { }, living in {}. I'm {} years old.", "name", "address", 1000);
  error("hi I'm {ab}, living in {}. I'm {} years old.", "name", "address",
        1000);

  LogLevel::set(3);
  println("LogLevel = {}", LogLevel::get());

  println("println test, no args");
  println("hi I'm {00}, living in {}. I'm {} years old.", "name", "address",
          1000);
  trace("hi I'm { d}, living in {}. I'm {} years old.", "name", "address",
        1000);
  info("hi I'm { d}, living in {}. I'm {} years old.", "name", "address", 1000);
  debug("hi I'm {ab}, living in {}. I'm {} years old.", "name", "address",
        1000);
  warn("hi I'm { }, living in {}. I'm {} years old.", "name", "address", 1000);
  error("hi I'm {ab}, living in {}. I'm {} years old.", "name", "address",
        1000);

  MYFS_ASSERT(1 > 2, "msg");
  return 0;
}
